﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Cartoons.Model;
using System.Collections.Generic;
using System.Threading;
using System.Text;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class BookService
{
    BooksDBEntities db = new BooksDBEntities();

    [OperationContract]
    public IEnumerable<int> GetBooks()
    {
//        return db.Books.Where(b => b.TitlePageIndex >= 0 && b.DzcFileLocation.Contains(".xml")).OrderBy(b => b.TitlePageIndex).Select(b => b.BookID);
        return db.Books.Where(b => b.TitlePageIndex >= 0).OrderBy(b => b.TitlePageIndex).Select(b => b.BookID);
    }

    //[OperationContract]
    //public string GetBooks()
    //{
    //    StringBuilder builder = new StringBuilder();
    //    var ids = db.Books.Where(b => b.TitlePageIndex >= 0 && b.DzcFileLocation.Contains(".xml")).Select(b => b.BookID);
    //    foreach (var item in ids)
    //    {
    //        builder.Append(item);
    //        builder.Append(';');
    //    }
    //    return builder.ToString();
    //}

    [OperationContract]
    public IEnumerable<Book> LoadBooksByID(IEnumerable<int> ids)
    {
        var ar = db.Books.Include("Cards").Include("Authors").Where(b => ids.Contains(b.BookID)).ToArray();
        return ar;
    }

    [OperationContract]
    public IEnumerable<Author> GetAuthorsList()
    {
        return db.Authors;
    }

    [OperationContract]
    public IEnumerable<string> GetBookNames()
    {
        return db.Books.Select(b => b.BookName);
    }
}
